package com.mofofa.maozy.components.path

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.mofofa.maozy.R


class MeAvatarPath : View {

    /**
     * 画笔
     */
    private lateinit var paint: Paint

    /**
     * 颜色
     */
    private var color = 0

    /**
     * 矩形高度占的比例
     */
    private var topRectWeight = 0

    /**
     * 圆弧高度占的比例
     */
    private var bottomArcWeight = 0

    /**
     * 圆弧高度占的比例
     */
    private lateinit var topRectF: RectF

    /**
     * 弧形 路径
     */
    private lateinit var path: Path

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let {
                it.obtainStyledAttributes(attrs, R.styleable.MeAvatarPath).apply {
                    color = getColor(
                        R.styleable.MeAvatarPath_arcRectViewColor,
                        ContextCompat.getColor(context, R.color.white)
                    )
                    topRectWeight =
                        getInt(R.styleable.MeAvatarPath_arcRectViewRectWeight, 5);

                    bottomArcWeight =
                        getInt(
                            R.styleable.MeAvatarPath_arcRectViewArcWeight,
                            1
                        );
                }
            }
        }

        // 设置画笔
        paint = Paint();
        paint.color = color;
        paint.isAntiAlias = true
        paint.style = Paint.Style.FILL

        // 设置 弧形和矩形实例
        topRectF = RectF()
        path = Path()
    }

    override fun onDraw(canvas: Canvas?) {
        // 全部高度 所占的比例 顶部矩形+底部圆弧
        val allWeight = topRectWeight + bottomArcWeight
        // 每一份的高度
        val eachHeight = height * 1F / allWeight

        println("height=$height")
        println("topRectWeight=$topRectWeight")
        println("bottomArcWeight=$bottomArcWeight")
        println("eachHeight=$eachHeight")
        println("============")

        // 矩形 的 上下左右
//        topRectF.left = 0F
//        topRectF.top = 0F
//        topRectF.right = width * 1F
//        topRectF.bottom = height * 1F / allWeight * topRectWeight
        topRectF.left = 0F
        topRectF.top = eachHeight * bottomArcWeight
        topRectF.right = width * 1F
        topRectF.bottom = height * 1F

        // 移动 到起点  根据比例计算 相当于 矩形底部 ，为了合理优化计算误差 贴合紧密 特意 -1 也就是向上移动一点点
//        path.moveTo(0F, height * 1F / allWeight * topRectWeight - 1)
//        path.quadTo(
//            width * 1F / 2,
//            height * 1F,
//            width * 1F,
//            height * 1F / allWeight * topRectWeight - 1
//        )
        path.moveTo(0F, eachHeight * bottomArcWeight)
        path.quadTo(
            width * 1F / 2,
            0F - (eachHeight / 2),
            width * 1F,
            eachHeight * bottomArcWeight
        )

        // 画顶部矩形
        canvas!!.drawRect(topRectF, paint)
        // 画弧形
        canvas.drawPath(path, paint)
    }

    /**
     * 代码设置 相关 UI
     *
     * @param topRectFWeight  顶部 矩形高度 比例
     * @param bottomArcWeight 底部 弧形高度 比例
     * @param color           整体颜色
     */
    fun resetArcView(topRectFWeight: Int, bottomArcWeight: Int, color: Int) {
        this.topRectWeight = topRectFWeight
        this.bottomArcWeight = bottomArcWeight
        this.color = color
        paint.color = color
        invalidate()
    }

    /**
     * 可设置背景颜色 跟随一定的规则 变化 重绘
     *
     * @param color 颜色
     */
    fun setPaintColor(color: Int) {
        paint.color = color
        invalidate()
    }
}